package com.ksd.modules.advert.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.annotation.ApiLog;
import com.ksd.common.controller.BaseController;
import com.ksd.common.core.result.R;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.common.web.annotation.RepeatSubmit;
import com.ksd.modules.advert.entity.AdminAdvertment;
import com.ksd.modules.advert.po.AdminAdvertMentSavePO;
import com.ksd.modules.advert.po.AdminAdvertMentSearchPO;
import com.ksd.modules.advert.service.IAdminAdvertisementService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 广告商 前端控制器
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-16
 */
@RestController
public class AdminAdvertmentController extends BaseController {

    @Autowired
    private IAdminAdvertisementService adminAdvertmentService;

    /**
     * 查询首页有广告商接口
     * @param adminAdvertMentSearchPO
     * @return
     */
    @PostMapping("/advertment/list")
    public R<Page<AdminAdvertment>> findAdminAdvertment(@Validated @RequestBody AdminAdvertMentSearchPO adminAdvertMentSearchPO){
        return R.data(adminAdvertmentService.findAdminAdvertment(adminAdvertMentSearchPO));
    }

    /**
     * 查询首页有广告商接口
     * @param adminAdvertMentSearchPO
     * @return
     */
    @PostMapping("/advertment/recyle")
    public R<Page<AdminAdvertment>> findAdminAdvertmentRecyle(@Validated @RequestBody AdminAdvertMentSearchPO adminAdvertMentSearchPO){
        adminAdvertMentSearchPO.setDeleted(DeletedEnums.DELETED.getCode());
        return R.data(adminAdvertmentService.findAdminAdvertment(adminAdvertMentSearchPO));
    }

    @PostMapping("/advertment/getList")
    public R<List<AdminAdvertment>> getList(){
        return R.data(adminAdvertmentService.getList());
    }

    /**
     * 保存和更新
     * @param adminAdvertMentSavePO
     * @return
     */
    @PostMapping("/advertment/saveupdate")
    @ApiLog("广告商保存和更新")
    @RepeatSubmit
    public R<Boolean> saveUpdateAdminAdvertment(@Validated @RequestBody AdminAdvertMentSavePO adminAdvertMentSavePO){
        return R.data(adminAdvertmentService.saveUpdateAdminAdvertment(adminAdvertMentSavePO));
    }

    /**
     * 根据id查询广告商
     * @param id
     * @return
     */
    @PostMapping("/advertment/get/{id}")
    public R<AdminAdvertment> getAdminAdvertmentById(@PathVariable("id")Long id){
        return R.data(adminAdvertmentService.getAdminAdvertmentById(id));
    }

    /**
     * 根据id删除广告商
     * @param id
     * @return
     */
    @PostMapping("/advertment/del/{id}")
    @ApiLog("根据id删除广告商")
    @RepeatSubmit
    public R<Boolean> delAdminAdvertmentById(@PathVariable("id")Long id){
        return R.data(adminAdvertmentService.deleteAdminAdvertmentById(id));
    }


    /**
     * 修改状态
     *
     * @return
     */
    @PostMapping("/advertment/status/{id}/{field}/{value}")
    @ApiLog("广告商修改状态")
    @RepeatSubmit
    public R<Boolean> updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("field") String field,
            @PathVariable("value") String value
    ) {
        if(StringUtils.isNumeric(value)) {
            return R.data(adminAdvertmentService.updateStatus(id, field, Integer.parseInt(value)));
        }else{
            return R.data(adminAdvertmentService
                    .updateStatusStr(id, field, String.valueOf(value)));
        }
    }

    /**
     * 【广告商】-  复制数据
     * @param opid
     * @return
     */
    @PostMapping("/advertment/copy/{opid}")
    @ApiLog("【广告商】- 复制数据")
    @RepeatSubmit
    public R<Boolean> copyData(@PathVariable("opid")Long opid){
        return R.data(adminAdvertmentService.copyData(opid));
    }
}
